#include "mainwindow1.h"
#include "ui_mainwindow1.h"

MainWindow1::MainWindow1(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow1)
{
    ui->setupUi(this);
    setWindowTitle("账号注册与登录系统");
    socket = new QTcpSocket(this);
    ui->lineEdit->setMinimumSize(200, 50);
    ui->lineEdit->setMaximumSize(10000, 100);
    ui->lineEdit_2->setMinimumSize(200, 50);
    ui->lineEdit_2->setMaximumSize(10000, 100);
    ui->pushButton->setMinimumSize(100, 40);
    ui->pushButton->setMaximumSize(10000, 40);
    ui->pushButton_2->setMinimumSize(100, 40);
    ui->pushButton_2->setMaximumSize(10000, 40);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(clickedSlot()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(clickedSlot1()));
    //xiesi
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(myslot()));
}

MainWindow1::~MainWindow1()
{
    delete ui;
}

void MainWindow1::setNetworkInfo(const QString &ip, const QString &port)
{
    QString wangguanip = ip;
    QString wangguanport = port;
    socket->connectToHost(wangguanip, wangguanport.toInt());
    connect(socket, &QTcpSocket::connected, this, &MainWindow1::onSocketConnected);
    connect(socket, &QTcpSocket::disconnected, this, &MainWindow1::onSocketDisconnected);
    connect(socket, &QTcpSocket::readyRead, this, &MainWindow1::onSocketReadyRead);
}

void MainWindow1::onSocketConnected()
{
    qDebug() << "已连接到服务器";
}

void MainWindow1::onSocketDisconnected()
{
    qDebug() << "已断开与服务器的连接";
}

void MainWindow1::onSocketReadyRead()
{
    QByteArray data = socket->readAll();
    qDebug() << "收到响应:" << data;
    QJsonParseError error;
    // 解析JSON响应
    QString jsonStr = QString::fromUtf8(data);
    QJsonDocument doc2 = QJsonDocument::fromJson(jsonStr.toUtf8(), &error);
    if (doc2.isNull() || !doc2.isObject()) {
        QMessageBox::warning(this, "错误", "收到无效的响应格式");
        return;
    }
    QJsonObject response = doc2.object();
    processResponse(response);
}

void MainWindow1::myslot()
{
    mainWindow2->show();
    this->close();
}

void MainWindow1::sendRequest(int order, const QString &username, const QString &password)
{
    if (socket->state() != QAbstractSocket::ConnectedState) {
        QMessageBox::warning(this, "错误", "未连接到服务器");
        return;
    }

    // 创建JSON对象
    QJsonObject json;
    json["order"] = order;
    json["user"] = username;
    json["password"] = password;

    // 转换为JSON文档
    QJsonDocument doc(json);
    QByteArray data = doc.toJson(QJsonDocument::Compact);

    // 发送数据
    socket->write(data);
    socket->flush();
    QMessageBox::information(this,"information", "注册成功");
    qDebug() << "已发送请求:" << data;
}

void MainWindow1::processResponse(const QJsonObject &response)
{
    if (response.contains("answer")) {
        QString answer = response["answer"].toString();

        if (answer == "yes") {
            QMessageBox::information(this, "成功", "成功登录");
            mainWindow2->show();
            this->close();
        } else {
            QMessageBox::warning(this, "失败", "登陆失败");
        }
    } else {
        QMessageBox::warning(this, "错误", "收到无效的响应格式123");
    }
}

void MainWindow1::clickedSlot1()
{
    zhanghao = ui->lineEdit->text();
    mima = ui->lineEdit_2->text();
    sendRequest(1, zhanghao, mima);
}

void MainWindow1::clickedSlot()
{

    zhanghao = ui->lineEdit->text();
    mima = ui->lineEdit_2->text();
    sendRequest(0, zhanghao, mima);
}
